package skeleton_wearebatman;

import java.io.BufferedReader; // import a beolvasáshoz
import java.io.IOException; // és a hibás beolvasás hibakezeléséhez

public class Circuit {

    private String name;  // a objektum-változó nevének eltárolásához kell

    public Circuit(String s, int choose) {
        BufferedReader keyboard = new BufferedReader(new java.io.InputStreamReader(System.in));
        name = s;
        System.out.println("CALL | Circuit | " + name + " | " + this.hashCode() + " | Circuit()");

        switch (choose) {
            case 1:
                // Alkatrészek létrehozása
                System.out.println("\nCreating circuit parts:");
                Switch sw = new Switch("sw");
                Wire w1 = new Wire("w1");
                Inverter inv = new Inverter("inv");
                Wire w2 = new Wire("w2");
                Led led = new Led("led");

                // Összekötés
                System.out.println("\nConnecting parts:");
                w1.connect(inv.getInput(0), sw.getOutput(0));
                w2.connect(led.getInput(0), inv.getOutput(0));

                // Indítás
                System.out.println("\nStarting:");
                sw.operate();
                w1.operate();
                inv.operate();
                w2.operate();
                led.operate();

                // Vége
                System.out.println("\nEnd of Test 1. Press Enter to continue.");
                try {
                    keyboard.readLine();
                } catch (IOException ex) {
                }

                break;
            case 2:
                // Alkatrészek létrehozása
                System.out.println("\nCreating circuit parts:");
                sw = new Switch("sw");
                w1 = new Wire("w1");
                led = new Led("led");

                // Összekötés
                System.out.println("\nConnecting parts:");
                w1.connect(led.getInput(0), sw.getOutput(0));

                // Indítás
                System.out.println("\nStarting:");
                sw.operate();
                w1.operate();
                led.operate();

                // Vége
                System.out.println("\nEnd of Test 2. Press Enter to continue.");
                try {
                    keyboard.readLine();
                } catch (IOException ex) {
                }
                break;
            case 3:
                // Alkatrészek létrehozása
                System.out.println("\nCreating circuit parts:");
                Generator gen = new Generator("gen");
                w1 = new Wire("w1");
                OR_Gate or1 = new OR_Gate("or1");
                w2 = new Wire("w2");
                inv = new Inverter("inv");
                Wire w3 = new Wire("w3");
                led = new Led("led");
                Wire w4 = new Wire("w4");

                gen.setData("");

                // Összekötés
                System.out.println("\nConnecting parts:");
                w1.connect(or1.getInput(0), gen.getOutput(0));
                w2.connect(inv.getInput(0), or1.getOutput(0));
                w3.connect(or1.getInput(1), inv.getOutput(0));
                w4.connect(led.getInput(0), w2.getOutput());

                // Indítás
                System.out.println("\nStarting:");
                gen.operate();
                w1.operate();
                System.out.print("DECIDE | Loop number: ");
                keyboard = new BufferedReader(new java.io.InputStreamReader(System.in));
                int n = 0;
                try {
                    n = Integer.parseInt(keyboard.readLine());
                } catch (IOException ex) {
                }
                for (int i = 0; i < n; i++) {
                    keyboard = new BufferedReader(new java.io.InputStreamReader(System.in));
                    int changed = 0;

                    // Bekérjük, hogy változott-e a bemenet
                    try {
                        do {
                            System.out.print("DECIDE | Has the or1.input changed (0-false; 1-true)? ");
                            changed = Integer.parseInt(keyboard.readLine());
                        } while (changed != 1 && changed != 0);
                    } catch (IOException ex) {
                    }

                    // Ha változott a bemenet akkor versenyhelyzet állt elő
                    if (changed == 1) {
                        System.out.println("Race condition!");
                        break;
                    }
                    or1.operate();
                    w2.operate();
                    inv.operate();
                    w3.operate();
                }
                w4.operate();
                led.operate();

                // Vége
                System.out.println("\nEnd of Test 3. Press Enter to continue.");
                try {
                    keyboard.readLine();
                } catch (IOException ex) {
                }

                break;
            case 4:
                // Alkatrészek létrehozása
                System.out.println("\nCreating circuit parts:");
                Wire w0 = new Wire("w0");
                gen = new Generator("gen");
                w1 = new Wire("w1");
                w2 = new Wire("w2");
                or1 = new OR_Gate("or1");
                AND_Gate and1 = new AND_Gate("and1");
                w3 = new Wire("w2");
                w4 = new Wire("w3");
                Inverter inv1 = new Inverter("inv1");
                Inverter inv2 = new Inverter("inv2");
                Wire w5 = new Wire("w5");
                led = new Led("led");
                OR_Gate or2 = new OR_Gate("or2");
                Wire w6 = new Wire("w6");

                gen.setData("");

                // Összekötés
                System.out.println("\nConnecting parts:");
                w0.connect(inv1.getInput(0), gen.getOutput(0));
                w1.connect(and1.getInput(0), inv1.getOutput(0));
                w2.connect(w1.getInput(), w1.getOutput());
                w3.connect(or2.getInput(0), or1.getOutput(0));
                w4.connect(or2.getInput(0), or1.getOutput(0));
                w5.connect(or2.getInput(1), and1.getOutput(0));
                w6.connect(led.getInput(0), or2.getOutput(0));

                // Indítás
                System.out.println("\nStarting:");
                gen.operate();
                w0.operate();
                inv1.operate();
                w1.operate();
                and1.operate();
                w5.operate();
                inv2.operate();
                w2.operate();
                or1.operate();
                w4.operate();
                or2.operate();
                w6.operate();
                led.operate();

                // Vége
                System.out.println("\nEnd of Test 4. Press Enter to continue.");
                try {
                    keyboard.readLine();
                } catch (IOException ex) {
                }

                break;
            default:
                break;
        }
    }

    public void tick() {
        System.out.println("CALL | Circuit | " + name + " | " + this.hashCode() + " | tick()");
    }

    public void init() {
        System.out.println("CALL | Circuit | " + name + " | " + this.hashCode() + " | init()");
    }
}
